package org.budo.gitee.api;

import org.budo.gitee.api.response.GiteeTokenResponse;
import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;
import org.budo.support.java.net.util.UrlEncodeUtil;

/**
 * @author lmw
 */
public class GiteeOauthApi extends AbstractGiteeApi {
    public GiteeTokenResponse token(String code) {
        String url = "https://gitee.com/oauth/token" //
                + "?grant_type=authorization_code" //
                + "&code=" + code //
                + "&client_id=" + this.getAppKey() //
                + "&redirect_uri=" + UrlEncodeUtil.encode(this.getRedirectUri()) //
                + "&client_secret=" + this.getAppSecret();

        Request tokenRequest = new Request();
        tokenRequest.setMethod(Request.POST);
        tokenRequest.setUrl(url);

        Response tokenResponse = tokenRequest.execute();
        return new GiteeTokenResponse(tokenResponse);
    }
}